/**
 * Project Name:filecontrol
 * File Name:EncryptPropertyPlaceholderConfigurer.java
 * Package Name:net.bwda.util.security
 * Date:2018年4月25日
 * Copyright (c) 2018, 江苏保旺达软件有限公司 All Rights Reserved.
 *
*/

package com.bwda.dsrs.filecenter.util.jinku;

import java.util.Properties;

import com.bwda.dsrs.base.jar.util.Des;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * springframework配置文件加密
 *
 * @author xufei
 * @date 2018年4月25日
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    /**
     * 需要解密的配置项名称
     */
    private String[] encryptPropNames = {"jdbc.url","jdbcFor4A.url","jdbcDes.url","jdbc.username", "jdbcDes.username", "jdbcFor4A.username", "jdbc.password",
        "jdbcDes.password", "jdbcFor4A.password","mq.brokerURL","mq.userName","mq.password"};
        
    /**
     * 对加密的配置进行解密处理
     * 
     * @param beanFactory 配置工厂
     * @param props 配置对象
     * @throws 处理异常
     */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
        throws BeansException {
        try {
            Des des = new Des();
            // 数据库配置解密
            for (int i = 0; i < encryptPropNames.length; i++) {
                String value = props.getProperty(encryptPropNames[i]);
                if (value != null) {
                    props.setProperty(encryptPropNames[i], des.decode(value));
                }
            }
            super.processProperties(beanFactory, props);
        }
        catch (Exception e) {
            throw new BeanInitializationException(e.getMessage());
        }
    }
    
}
